﻿#include "Graphics42ValveItem.h"
#include <QBrush>
#include <QPen>
#include <QPainter>
#include <QDebug>
#define _USE_MATH_DEFINES
#include <cmath>

#define M_PI       3.14159265358979323846

Graphics42ValveItem::Graphics42ValveItem()
    : GraphicsAirValveItem()
{
    m_keyPoints[0] = QPoint(-25, -35-17);
    m_keyPoints[1] = QPoint(25, -35-17);
    m_keyPoints[2] = QPoint(-25, 25);
    m_keyPoints[3] = QPoint(25, 25);
}

QPointF Graphics42ValveItem::keyPoint(int i)
{

    return mapToScene(m_keyPoints[i]);
}

QRectF Graphics42ValveItem::boundingRect() const
{
    int width = 51 + 34 + 5;
    int height = 34 + 50 + 20;
    return QRectF(-width / 2.0, -35 - 17 - 3, width, height);
}

void Graphics42ValveItem::setEnable(bool enable)
{
    m_enable = enable;
    update();
}

void Graphics42ValveItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen penOn(Qt::black), penOff(Qt::red);
    penOn.setWidth(3);
    penOff.setWidth(3);

    if(m_enable)
    {
        painter->save();
        painter->translate(0, -30);
        drawStateOff(painter, Right);
        painter->translate(-25, 35);
        drawStateOn(painter, Down);
        painter->translate(2 * 25, 0);
        drawStateOn(painter, Up);
        painter->restore();
    }
    else
    {
        painter->save();
        painter->translate(0, -35);
        drawStateOn(painter, Right);
        painter->translate(-25, 35);
        drawStateOff(painter, Up);
        painter->translate(2.0 * 25, 0);
        drawStateOff(painter, Up);
        painter->restore();
    }
    painter->setPen(penOn);
    painter->drawLine(QLine(-25, -35-17, -25, -21));
    painter->drawLine(QLine(25, -35-17, 25, -21));

}
